/**
 * Yarbu is a proof of concept of a JavaScript HTTP server.
 * Copyright (C) 2011 Antonio Hernandez Diaz <ahdiaz@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $Id: ConnectionFactory.java 2 2011-01-08 06:03:19Z ahdiaz $
 */

package net.ahdiaz.yarbu.sql;

import java.sql.DriverManager;

import org.mozilla.javascript.ScriptableObject;

public class ConnectionFactory extends ScriptableObject {

	private static final long serialVersionUID = -1915969788779519079L;
	
	private String driver = null;
	private String className = null;
	
	public ConnectionFactory() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public String getClassName() {
		return "ConnectionFactory";
	}
	
	public void jsConstructor(String driver) throws ClassNotFoundException {
		if (driver.equals("sqlite")) {
			this.driver = driver;
			this.className = "org.sqlite.JDBC";
		}
		// etc...
		Class.forName(this.className);
	}
	
	public java.sql.Connection jsFunction_getConnection(String url) throws Exception {
		java.sql.Connection conn = DriverManager.getConnection("jdbc:%driver:%url".replaceAll("%driver", driver).replaceAll("%url", url));
		return conn;
	}

}
